package com.cocotingo.snail.template.models;

import com.cocotingo.snail.template.TemplateSyntaxException;

public abstract class IDNode extends Node {
	
	public static boolean isValidID(String id) {
		char ch;
		if (id == null || id.isEmpty()) return false;
		for (int i = 0; i < id.length(); i++) {
			ch = id.charAt(i);
			if (i == 0) { if (!isLetter(ch) && ch != '_') return false; }
			else { if (!isLetter(ch) && !isDigit(ch) && ch != '_') return false; }
		}
		if (id.equals("null")) return false;
		return true;
	}
	
	public static boolean isLetter(char ch) {
		return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
	}
	
	public static boolean isDigit(char ch) {
		return ch >= '0' && ch <= '9';
	}
	
	private String id;
	
	public IDNode() {
	}
	
	public String getID() {
		return id;
	}
	
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public void validateNode() throws TemplateSyntaxException {
		if (!isValidID(id)) throw new TemplateSyntaxException(this, "Invalid ID: " + id + ".");
	}
	
}
